package com.ruicar.afs.cloud.channel.grade.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruicar.afs.cloud.components.datadicsync.dto.DicDataDto;
import lombok.experimental.UtilityClass;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * @Description:字典翻译工具
 * @param:
 * @return:
 * @auther: jiangxiongfei
 * @date: 2020-09-03 15:06
 */
@UtilityClass
public class DicUtils {

    /**
     * @Description:DicUtils
     * @param:
     * @return:
     * @auther: jiangxiongfei
     * @date: 2020-09-03 15:06
     */
    public String translate(String dataKey, Map<String, List<DicDataDto>> dicMap) {
        String translatedTitle = "";
        for (Map.Entry<String, List<DicDataDto>> entry : dicMap.entrySet()) {
            if (CollectionUtil.isNotEmpty(entry.getValue())) {
                for (DicDataDto dicDataDto : entry.getValue()) {
                    if (dicDataDto.getValue().equals(dataKey)) {
                        translatedTitle = dicDataDto.getTitle();
                        break;
                    }
                }
            }
            if (StrUtil.isNotBlank(translatedTitle)) {
                break;
            }
        }
        return translatedTitle;
    }

    /**
     * @Description: 左边补0
     * @param: [a-初始数据, j-补0后总长度]
     * @return: java.lang.String
     * @auther: jiangxiongfei
     * @date: 2020-08-19 10:39
     */
    public static String intToString(long a, int j) {
        DecimalFormat df = new DecimalFormat(initString('0', j));
        return df.format(a);
    }

    /**
     * @Description: 初始化字符串
     * @param: [c-补充的字符, a-补充后的长度]
     * @return: java.lang.String
     * @auther: jiangxiongfei
     * @date: 2020-08-19 10:42
     */
    public static String initString(char c, int a) {
        try {
            if (a < 0) {
                return "";
            }
        } catch (IllegalArgumentException var5) {
            throw var5;
        }

        char[] arr = new char[c];
        int i = 0;

        while (true) {
            try {
                if (i >= a) {
                    return new String(arr);
                }
            } catch (IllegalArgumentException var4) {
                throw var4;
            }
            arr[i] = c;
            ++i;
        }
    }
}
